package com.realshare.frame.util.excel;


import com.realshare.frame.util.excel.common.Callback;
import com.realshare.frame.util.excel.model.ExcelEntity;

import java.awt.*;
import java.io.File;
import java.util.List;


public class ExcelUtils {

    private static final String EMPTY = "";
    private static final String POINT = ".";

    /**
     * get postfix of the path
     *
     * @param path 路径
     * @return 返回后缀
     */
    public static String getPostfix(String path) {
        if (path == null || EMPTY.equals(path.trim())) {
            return EMPTY;
        }
        if (path.contains(POINT)) {
            return path.substring(path.lastIndexOf(POINT) + 1, path.length());
        }
        return EMPTY;
    }

    /**
     * 用于将Excel表格中列号字母转成列索引，从0对应A开始
     *
     * @param column 列号
     * @return 列索引
     */
    public static int columnToIndex(String column) {
        int index = 0;
        if (column.matches("[A-Z]+")) {
            char[] chars = column.toUpperCase().toCharArray();
            for (int i = 0; i < chars.length; i++) {
                index += ((int) chars[i] - (int) 'A' + 1)
                        * (int) Math.pow(26, chars.length - i - 1) - 1;
            }
        }
        return index;
    }

    /**
     * 用于将excel表格中列索引转成列号字母，从A对应0开始
     *
     * @param index 列索引
     * @return 列号
     */
    public static String indexToColumn(int index) {
        String column = "";
        if (index > 0) {
            index--;
            do {
                if (column.length() > 0) {
                    index--;
                }
                column = ((char) (index % 26 + (int) 'A')) + column;
                index = ((index - index % 26) / 26);
            } while (index > 0);
        }
        return column;
    }

    /**
     * Color对象转换成字符串
     *
     * @param color Color对象
     * @return 16进制颜色字符串
     */
    public static String toHexFromColor(Color color) {
        String r, g, b;
        StringBuilder su = new StringBuilder();
        r = Integer.toHexString(color.getRed());
        g = Integer.toHexString(color.getGreen());
        b = Integer.toHexString(color.getBlue());
        r = r.length() == 1 ? "0" + r : r;
        g = g.length() == 1 ? "0" + g : g;
        b = b.length() == 1 ? "0" + b : b;
        r = r.toUpperCase();
        g = g.toUpperCase();
        b = b.toUpperCase();
        su.append("0xFF");
        su.append(r);
        su.append(g);
        su.append(b);
        return su.toString();
    }

    /**
     * 字符串转换成Color对象
     *
     * @param colorStr 颜色字符串
     * @return Color对象
     */
    public static Color toColorFromString(String colorStr) {
        return new Color(Integer.parseInt(colorStr, 16));
    }

    /**
     * 读取excel
     *
     * @param path 文件路径
     * @throws Exception
     */
    public static List<List<String>> readExcel(String path) throws Exception {
        ReadExcel readExcel = new ReadExcel();
        return readExcel.readExcel(path, 1);
    }

    /**
     * 读取excel
     *
     * @param path     文件路径
     * @param sheetNum sheet索引
     * @throws Exception
     */
    public static List<List<String>> readExcel(String path, int sheetNum) throws Exception {
        ReadExcel readExcel = new ReadExcel();
        return readExcel.readExcel(path, sheetNum);
    }

    /**
     * 读取excel
     *
     * @param path     文件路径
     * @param callback 回调
     * @throws Exception
     */
    public static List<List<String>> readExcel(String path, Callback callback) throws Exception {
        ReadExcel readExcel = new ReadExcel();
        readExcel.setCallback(callback);
        return readExcel.readExcel(path, 1);
    }

    /**
     * 读取excel
     *
     * @param path     文件路径
     * @param sheetNum sheet索引
     * @param callback 回调
     * @throws Exception
     */
    public static List<List<String>> readExcel(String path, int sheetNum, Callback callback) throws Exception {
        ReadExcel readExcel = new ReadExcel();
        readExcel.setCallback(callback);
        return readExcel.readExcel(path, sheetNum);
    }

    /**
     * 复制excel
     *
     * @param sourcePath 原路径
     * @param modelPath  模板路径
     * @return
     */
    public static boolean writeByModel(String sourcePath, String modelPath, List<List<String>> rowList) {
        WriteExcel writeExcel = new WriteExcel();
        return writeExcel.writeByModel(sourcePath, modelPath, rowList);
    }

    public static boolean writeByModel(String sourcePath, String modelPath) {
        WriteExcel writeExcel = new WriteExcel();
        return writeExcel.writeByModel(sourcePath, modelPath);
    }

    /**
     * 通用导出excel
     *
     * @param entity 需要导出的数据
     */
    public static File writeExcel(ExcelEntity entity) {
        WriteExcel writeExcel = new WriteExcel();
        return writeExcel.writeExcel(entity);
    }

    public static void main(String[] args) throws Exception {
        String sourcePath = "D:/1.xlsx";
        String modelPath = "D:/2.xlsx";
        String testPath = "D:/3.xls";
        List<List<String>> rowList = ExcelUtils.readExcel(sourcePath);
        for (List<String> row : rowList) {
            for (String cell : row) {
                System.out.println("1=====" + cell);
            }
        }
        rowList = ExcelUtils.readExcel(testPath);
        for (List<String> row : rowList) {
            for (String cell : row) {
                System.out.println("2=====" + cell);
            }
        }
        //ExcelUtils.writeByModel(sourcePath, modelPath);
    }


}
